/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers.Beans;

import Entities.Book;
import Entities.Rol;
import Entities.Student;
import Entities.User;
import Facades.BookFacade;
import Facades.RolFacade;
import Facades.StudentFacade;
import Facades.UserFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;

/**
 *
 * @author roycalderon
 */
@Named(value = "loanBean")
@SessionScoped
public class LoanBean implements Serializable {

    private DualListModel<Book> dl_availableBooks;
    //Para el Growwwwl
    private ExternalContext ex = FacesContext.getCurrentInstance().getExternalContext();
    //Entidades
    private Book e_Book;
    private User e_User;
    private Student e_Student;
    //Facades
    @EJB
    private UserFacade ejbFacade_User;
    @EJB
    private StudentFacade ejbFacade_Student;
    @EJB
    private BookFacade ejbFacade_Book;

    public LoanBean() {
        if (e_Book == null) {
            e_Book = new Book();
        }

        List<Book> l_AvailableBooks = ejbFacade_Book.getAllAvailablesBooks();
        List<Book> l_PickedBooks = new ArrayList<Book>();

        for (Book objBook : l_AvailableBooks) {
            e_Book=objBook;
        }
        
        dl_availableBooks = new DualListModel<Book>(l_AvailableBooks, l_PickedBooks);
        
        
    }

    public ExternalContext getEx() {
        return ex;
    }

    public void setEx(ExternalContext ex) {
        this.ex = ex;
    }

    public User getE_User() {
        return e_User;
    }

    public void setE_User(User e_User) {
        this.e_User = e_User;
    }

    public Student getE_Student() {
        return e_Student;
    }

    public void setE_Student(Student e_Student) {
        this.e_Student = e_Student;
    }

    public UserFacade getEjbFacade_User() {
        return ejbFacade_User;
    }

    public void setEjbFacade_User(UserFacade ejbFacade_User) {
        this.ejbFacade_User = ejbFacade_User;
    }

    public StudentFacade getEjbFacade_Student() {
        return ejbFacade_Student;
    }

    public void setEjbFacade_Student(StudentFacade ejbFacade_Student) {
        this.ejbFacade_Student = ejbFacade_Student;
    }
    
    

    public DualListModel<Book> getDl_availableBooks() {
        return dl_availableBooks;
    }

    public void setDl_availableBooks(DualListModel<Book> dl_availableBooks) {
        this.dl_availableBooks = dl_availableBooks;
    }

    public BookFacade getEjbFacade_Book() {
        return ejbFacade_Book;
    }

    public void setEjbFacade_Book(BookFacade ejbFacade_Book) {
        this.ejbFacade_Book = ejbFacade_Book;
    }

    public Book getE_Book() {
        return e_Book;
    }

    public void setE_Book(Book e_Book) {
        this.e_Book = e_Book;
    }

    public void onTransfer(TransferEvent event) {
        StringBuilder builder = new StringBuilder();
        for (Object item : event.getItems()) {
            builder.append(((Book) item).getName()).append("<br />");
        }

        FacesMessage msg = new FacesMessage();
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        msg.setSummary("Items Transferred");
        msg.setDetail(builder.toString());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
